package com.example.networkrequestencapsulation.network;

import android.text.TextUtils;
import com.example.networkrequestencapsulation.callback.JsonCallback;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者 : chenyixi
 * 日期 : 2019/11/19 18:27
 * 文件包名 : com.example.networkrequestencapsulation.network
 * 描述 :
 */
public class ApiUtils {

    public static final int API_UTILS_POST = 10001;
    public static final int API_UTILS_POST_BODY = 10002;
    public static final int API_UTILS_POST_UPLOAD_FILE = 10003;
    public static final int API_UTILS_GET = 20001;

    private static ApiUtils instance;

    private ApiUtils() {
    }

    public static ApiUtils getInstance() {
        if (null == instance) {
            instance = new ApiUtils();
        }
        return instance;
    }

    /**
     * 历史上的今天
     */
    public void getTOH(String key, int month, int day, JsonCallback jsonCallback) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("key", key);
        paramsMap.put("v", "1.0");
        paramsMap.put("month", month);
        paramsMap.put("day", day);
        this.unifiedEntrance(jsonCallback, ApiConstant.TOH, paramsMap, "getTOH", API_UTILS_POST);
    }


    public void unifiedEntrance(JsonCallback jsonCallback, String apiUrl, Map<String, Object> paramsMap, String cacheKey, int type) {

        switch (type) {
            case API_UTILS_POST:
                post(jsonCallback, apiUrl, paramsMap, cacheKey);
                break;
            case API_UTILS_POST_BODY:
                postBody(jsonCallback, apiUrl, paramsMap, cacheKey);
                break;
            case API_UTILS_POST_UPLOAD_FILE:
                postUploadFile(jsonCallback, apiUrl, paramsMap, cacheKey);
                break;
            case API_UTILS_GET:
                get(jsonCallback, apiUrl, paramsMap, cacheKey);
                break;
        }

    }


    //post方式访问后台
    private void post(JsonCallback okGoCallBack, String apiUrl, Map<String, Object> paramsMap, String cacheKey) {
        if (null == paramsMap) {
            paramsMap = new HashMap<>();
        }
        PostRequest postRequest = OkGo.post(apiUrl);

        if (!TextUtils.isEmpty(cacheKey)) {
            postRequest.cacheKey(cacheKey);
        }

        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            if (!TextUtils.isEmpty(entry.getKey()) && null != entry.getValue()) {
                postRequest.params(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
        // 执行
        postRequest.execute(okGoCallBack);

    }


    //post方式访问后台
    private void postUploadFile(JsonCallback okGoCallBack, String apiUrl, Map<String, Object> paramsMap, String cacheKey) {
        if (null == paramsMap) {
            paramsMap = new HashMap<>();
        }
        PostRequest postRequest = OkGo.post(apiUrl);
        postRequest.getHeaders().put("Content-Type", "multipart/form-data");
        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            if (!TextUtils.isEmpty(entry.getKey()) && null != entry.getValue()) {
                postRequest.addFileParams(entry.getKey(), (List<File>) entry.getValue());
            }
        }
        // 执行
        postRequest.execute(okGoCallBack);

    }


    //post方式访问后台
    private void postBody(JsonCallback okGoCallBack, String apiUrl, Map<String, Object> paramsMap, String cacheKey) {
        if (null == paramsMap) {
            paramsMap = new HashMap<>();
        }
        PostRequest postRequest = OkGo.post(apiUrl);
        if (!TextUtils.isEmpty(cacheKey)) {
            postRequest.cacheKey(cacheKey);
        }
        JSONObject json = new JSONObject(paramsMap);
        postRequest.upRequestBody(RequestBody.create(MediaType.parse("application/json"), String.valueOf(json)));
        // 执行
        postRequest.execute(okGoCallBack);

    }


    private void get(JsonCallback okGoCallBack, String apiUrl, Map<String, Object> paramsMap, String cacheKey) {
        if (null == paramsMap) {
            paramsMap = new HashMap<>();
        }

        GetRequest getRequest = OkGo.get(apiUrl);
        if (!TextUtils.isEmpty(cacheKey)) {
            getRequest.cacheKey(cacheKey);
        }
        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            if (!TextUtils.isEmpty(entry.getKey()) && null != entry.getValue()) {
                getRequest.params(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }

        // 执行
        getRequest.execute(okGoCallBack);
    }

}

